package com.ifonly.utils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:ifonlymaster@163.com">ifonly</a>
 * @version 1.0 2015-12-18 14:23
 * @since JDK 1.6
 */
public class ClassUtils {

    @SuppressWarnings("unchecked")
    public static <T> Class<T> loadClass(String className) {
        try {
            return (Class<T>) Class.forName(className);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static List<Field> findAnnotationFields(Class<? extends Annotation> annotationClass, Class<?> clazz) {
        try {
            List<Field> result = new ArrayList<Field>();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field: fields) {
                Annotation annotation = field.getAnnotation(annotationClass);
                if (annotation != null) {
                    result.add(field);
                }
            }
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
